package com.ruoyi.flowable.listener.move.moveRepair;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.equ.domain.EquMove;
import com.ruoyi.equ.domain.EquRepair;
import com.ruoyi.equ.service.IEquRepairService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.flowable.listener.repair.RepairExecutionListener.getRepairTime;

/**
 * 移动-维修-3监听
 */
@Slf4j
@Component("moveRepairListener_3")
public class MoveRepairListener_3 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //维修执行信息
            String wrongLocation = execution.getVariable("wrongLocation", String.class);
            String firstConfirm = execution.getVariable("firstConfirm", String.class);
            List<String> wrongReason = (List<String>) execution.getVariable("wrongReason");
            List<String> repairStep = (List<String>) execution.getVariable("repairStep");
            //TODO 补充部件
            List<Map<String, Object>> materialSubForm = (List<Map<String, Object>>) execution.getVariable("materialSubForm");
            List<Map<String, Object>> parts = new ArrayList<>();
            materialSubForm.forEach(item -> {
                HashMap<String, Object> data = new HashMap<>();
                data.put("材料/部件名称", item.get("materialName"));
                data.put("型号规格", item.get("modelSpec"));
                data.put("数量", item.get("quantity"));
                data.put("单价", item.get("unitPrice"));
                data.put("金额", item.get("amount"));
                data.put("供应商", item.get("supplier"));
                parts.add(data);
            });
            List<String> repairDuration = (List<String>) execution.getVariable("repairTime");
            String repairTime = getRepairTime(repairDuration);
            String allAmount = execution.getVariable("allAmount", String.class);

            //移动-执行信息
            EquMove equMove = execution.getVariable("equMove", EquMove.class);
            Map<String, Object> executeInfoMap = objectMapper.readValue(equMove.getExecuteInfo(), new TypeReference<Map<String, Object>>() {});
            HashMap<String, Object> dataMap = new HashMap<>();
            dataMap.put("故障位置", wrongLocation);
            dataMap.put("初步判断", firstConfirm);
            dataMap.put("故障原因", wrongReason);
            dataMap.put("维修措施", repairStep);//维修内容
            dataMap.put("部件清单", parts);
            dataMap.put("维修时长", repairTime);
            dataMap.put("维修费用", allAmount);//维修金额
            executeInfoMap.put("维修过程记录", dataMap);
            String executeInfo = objectMapper.writeValueAsString(executeInfoMap);

            equMove.setExecuteInfo(executeInfo);
            execution.setVariable("equMove", equMove);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
